<!-- 归一化弹窗 -->
<template>
  <div class="conindsys-add-or-update">
    <el-dialog :title="title" :visible.sync="dialogVisible" width="25vw" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="方法名称">
          <el-select v-model="funName" placeholder="请选择方法">
            <el-option v-for="item in funNameLists" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转换方法">
          <el-select v-model="funInvert" placeholder="转换方法">
            <el-option v-for="item in funInvertLists" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方法类型">
          <el-select v-model="form.normalizationMethod" placeholder="转换方法">
            <el-option v-for="item in normalizationMethodtLists" :key="item.value" :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方法描述" v-if="form.normalizationMethod !== undefined">
          <img :src="chosefunImg(form.normalizationMethod)" style="border-radius: 8px; width: 75%; height: 75%" />
        </el-form-item>
        <el-form-item label="方法参数" label-position="top" v-if="form.normalizationMethod !== undefined">
          <div style="display: flex; margin-bottom: 5px;" v-for="(item, index) in chosefunParams(form.normalizationMethod)" :key="index">
              <span style="width: 40px">{{ item }}:</span>
              <el-input-number controls-position="right" v-model="form.normalizationParameter[item]" placeholder="请输入参数" @input="$forceUpdate()" />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer loc-center">
        <el-button class="jtyj-btn-cancel" @click="dialogVisible = false">取消</el-button>
        <el-button class="jtyj-btn-primary"  :loading="disabledSubmit" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { updateZbtxData } from "@/api/iep/iep";
export default {
  data() {
    return {
      // 弹出层标题
      title: "归一化参数信息",
      dialogVisible: false, // 是否显示弹出层
      disabledSubmit: false,
      validFlag: true, // 判断格式校验是否通过
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 方法名称列表（暂时写死）
      funNameLists: [{ label: "归一化方法", value: "归一化方法" }],
      // 转化方法（暂时写死）
      funInvertLists: [{ label: "线性变换", value: "线性变换" }],
      normalizationMethodtLists: [
        {
          label: "效益型",
          value: "linearTransformationBenefitType",
        },
        {
          label: "成本性",
          value: "linearTransformationCostType",
        },
      ],
      funName: "归一化方法",
      funInvert: "线性变换",
      // 方法描述图片
      funImg: {
        linearTransformationBenefitType: require("/public/images/iep/xiaoyi.png"),
        linearTransformationCostType: require("/public/images/iep/chengben.png"),
      },
      // 方法参数（暂时写死）
      funParams: {
        linearTransformationBenefitType: ["a1", "a2"],
        linearTransformationCostType: ["a1", "a2"],
      },
    };
  },
  methods: {
    init(row) {
      this.dialogVisible = true;
      this.reset();
      if (row) {
        this.form.normalizationMethod = row.normalizationMethod;
        this.form.normalizationParameter = JSON.parse(
          row.normalizationParameter
        );
      }
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        normalizationMethod: undefined,
        normalizationParameter: {},
      };
      this.$nextTick(() => {
        this.resetForm("form");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        // 验证已选择方法类型
        if (this.form.normalizationMethod !== undefined) {
          let errArr = [];
          // 循环获取未填的参数列表
          this.funParams[this.form.normalizationMethod].map((item) => {
            if (this.form.normalizationParameter[item] === undefined) {
              errArr.push(item);
            }
          });
          if (errArr.length > 0) {
            this.$message.error("请输入" + errArr + "参数");
          } else {
            // 改变格式
            this.form.normalizationParameter = JSON.stringify(
              this.form.normalizationParameter
            );
            this.$emit("submit", this.form);
            this.dialogVisible = false;
          }
        } else {
          this.$message.error("请选择方法类型");
        }
      });
    },
    // 展示方法描述图片
    chosefunImg(normalizationMethod) {
      return this.funImg[normalizationMethod];
    },
    // 展示方法参数
    chosefunParams(normalizationMethod) {
      return this.funParams[normalizationMethod];
    },
  },
};
</script>

<style scoped></style>